<?php


namespace Modules\Common\Helpers\Traits\QueryBuilder;


use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\QueryBuilder;

trait Filters {
    protected $qb;

    protected function queryBuilder($items): self {
        $this->qb = QueryBuilder::for($items);
        return $this;
    }

    protected function with($relations, $callback = null): self {
        $this->qb = $this->qb->with($relations, $callback);
        return $this;
    }

    protected function aFilters($filters = []): self {
        $this->qb = $this->qb
            ->allowedFilters(array_merge([
                                             AllowedFilter::exact('id'),
                                             AllowedFilter::exact('course_id'),
                                             AllowedFilter::exact('course_catalogue_id'),

                                             AllowedFilter::exact('uid'),
                                             AllowedFilter::exact('category_id'),
                                             AllowedFilter::exact('parent_id'),
                                             AllowedFilter::exact('sort'),
                                             AllowedFilter::exact('week'),
                                             AllowedFilter::exact('level'),
                                             AllowedFilter::exact('code'),
                                             AllowedFilter::exact('is_inventory'),
                                             AllowedFilter::exact('on_sale'),
                                             AllowedFilter::exact('audit_status'),
                                             AllowedFilter::exact('nursery_school_id'),
                                             AllowedFilter::exact('enable_status'),

                                             AllowedFilter::exact('status'),
                                             AllowedFilter::exact('pay_status'),
                                             AllowedFilter::exact('trade_status'),
                                             'title', 'intro', 'content', 'reason', 'name',


                                             AllowedFilter::scope('CreatedAtOfDay'),
                                             AllowedFilter::scope('CreatedAtBefore'),
                                             AllowedFilter::scope('CreatedAtAfter'),
                                             AllowedFilter::scope('CreatedAtBetween'),

                                             AllowedFilter::scope('UpdatedAtBefore'),
                                             AllowedFilter::scope('UpdatedAtAfter'),
                                             AllowedFilter::scope('UpdatedAtBetween'),

                                             AllowedFilter::scope('ReviewAtBefore'),
                                             AllowedFilter::scope('ReviewAtAfter'),
                                             AllowedFilter::scope('ReviewAtBetween'),

                                             AllowedFilter::scope('PriceBefore'),
                                             AllowedFilter::scope('PriceAfter'),
                                             AllowedFilter::scope('PriceBetween'),

                                             AllowedFilter::scope('OriginalPriceBefore'),
                                             AllowedFilter::scope('OriginalPriceAfter'),
                                             AllowedFilter::scope('OriginalPriceBetween'),


                                             AllowedFilter::scope('StockBefore'),
                                             AllowedFilter::scope('StockAfter'),
                                             AllowedFilter::scope('StockBetween'),

                                             AllowedFilter::scope('SalesBefore'),
                                             AllowedFilter::scope('SalesAfter'),
                                             AllowedFilter::scope('SalesBetween'),

                                             AllowedFilter::scope('VirtualSalesBefore'),
                                             AllowedFilter::scope('VirtualSalesAfter'),
                                             AllowedFilter::scope('VirtualSalesBetween'),


                                             AllowedFilter::exact('address.province_code'),
                                             AllowedFilter::exact('address.city_code'),
                                             AllowedFilter::exact('address.area_code'),
                                             AllowedFilter::exact('address.zip'),
                                             AllowedFilter::exact('address.is_default'),
                                             AllowedFilter::scope('address.searchAddress'),


                                             'addresses.name',
                                             'addresses.province_name',
                                             'addresses.city_name',
                                             'addresses.area_name',
                                             'addresses.address',
                                             'addresses.phone',
                                             'addresses.tel',

                                             AllowedFilter::exact('addresses.province_code'),
                                             AllowedFilter::exact('addresses.city_code'),
                                             AllowedFilter::exact('addresses.area_code'),
                                             AllowedFilter::exact('addresses.zip'),
                                             AllowedFilter::exact('addresses.is_default'),
                                             AllowedFilter::scope('addresses.searchAddress'),
                                         ], $filters));
        return $this;
    }
}
